﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

public class UIMission : MonoBehaviour
{
    public MissionsScrollView missionScrollView;

    private List<MissionData> listMissions;

    public GameObject noMission;
    private void Awake()
    {
        missionScrollView.onValueChange.AddListener(MissionlueChangeHandler);
        noMission.SetActive(false);
    }

    private void OnEnable()
    {
        Refesh();
    }

    private void Update()
    {
        if (GameData.isMissionChanged)
        {
            Refesh();
            GameData.isMissionChanged = false;
        }
    }

    private void MissionlueChangeHandler(MissionItem missionItem)
    {
        UIMissionNameRender itemRender = missionItem.transform.GetComponent<UIMissionNameRender>();
        int[] missions = GetMissionTask();
        itemRender.SetData(listMissions[missions[missionItem.index]]);
    }

    public void Refesh()
    {
        listMissions = GameData._curMission;
        int missionCount = listMissions.Count;  
        for (int i = 0; i < listMissions.Count; i++)
        {
            if (listMissions[i].isFinish)
            {
                missionCount--;
            }
        }
        missionScrollView.SetCount(missionCount);
        noMission.SetActive(false);
        if (missionCount == 0)
        {
            noMission.SetActive(true);
        }
    }

    private int[] GetMissionTask()
    {
        listMissions = GameData._curMission;
        int[] ints = new int[listMissions.Count];
        int v = 0;
        for (int i = 0; i < listMissions.Count; i++)
        {
            if (listMissions[i].isFinish == false)
            {
                ints[v] = i;
                v++;
            }
        }
        return ints;
    }

}

